﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using ESF.General.Entity;
using System.Collections.Generic;

#endregion


namespace ESF.General.MVP
{
	public class BaseBrowsePagedPresenter<O, IO> : BasePresenter<IO> 
		where IO : IBaseBrowsePagedViewer<O> 
	{
		#region Propiedades
		
		protected PaggingInfo CurrentPaggingInfo
		{
			get
			{
				return _Viewer.frmPaggingInfo;
			}
		}
		
		
		#endregion
		
	
		#region Constructor
		
		public BaseBrowsePagedPresenter (IO pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones

		public override void Load()
		{
			
		}


		public override void Clear()
		{
			_Viewer.Results = null;
		}


		protected virtual void AfterBrowse(O pFilter, PaggingInfo pPagInfo) 
		{ 
			_Viewer.AfterBrowse (pFilter, pPagInfo);
		}


		protected virtual void AfterDoBrowse(O pFilter, PaggingInfo pPagInfo)
		{
			_Viewer.AfterDoBrowse(pFilter, pPagInfo);
		}
		
		
		protected virtual BrowseResults<O> GetPagedResults (PaggingInfo pPaggingInfo, out O pFilter) 
		{
			pFilter = default (O);

			return null;
		}

		
		public virtual void Delete (List<O> pSelectedItems) {}
		
		
		public void Delete ()
		{
			List<O> toDeleteItems;
			
			
			toDeleteItems = _Viewer.SelectedItems;
			
			if (DataHelper.IsEmpty (toDeleteItems)) 
            { 
                _Viewer.frmErrorMsg = "Seleccione al menos un elemento para borrar"; 
                return; 
            }
			
			try
			{
				Delete (toDeleteItems);
				
				SetDeletedMsg ();
				
				Browse ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public void Browse(bool pFromButton = false)
		{
			O filter;

			PaggingInfo pagInfo;


			try
			{
				pagInfo = CurrentPaggingInfo;

				_Viewer.Results = GetPagedResults(pagInfo, out filter);

				AfterBrowse(filter, pagInfo);

				if (pFromButton)
				{
					AfterDoBrowse(filter, pagInfo);
				}
			}
			catch (Exception pEx)
			{
				LogError(pEx); 
				
				_Viewer.Results = null;
			}
		}
		
		#endregion
	}
}
